<template>
  <div class="app-container">
    <div class="filter-container clearfix">
      <el-select v-model="listQuery.type" class="filter-item" style="width: 130px;" filterable clearable :loading="loading" placeholder="选择类型">
        <el-option label="禁言" value="1" />
        <el-option label="封账号" value="2" />
        <el-option label="封角色" value="3" />
        <el-option label="封IP" value="4" />
        <el-option label="无感禁言" value="5" />
        <el-option label="踢玩家下线" value="6" />
      </el-select>
      <el-date-picker v-model="listQuery.ban_dt[0]" class="filter-item" type="date" style="width: 140px;" placeholder="操作开始时间"></el-date-picker>-
      <el-date-picker v-model="listQuery.ban_dt[1]" class="filter-item" type="date" style="width: 140px;" placeholder="操作结束时间"></el-date-picker>
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button v-if="checkPermission(permissionRoles.ban_add)" class="filter-item" type="primary" icon="el-icon-plus" @click="handleAdd">添加封禁</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="730">
      <el-table-column label="操作类型" prop="type" width="90px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.type | typeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务器" prop="sname" align="center" width="100px"></el-table-column>
      <el-table-column label="账号" prop="account" width="160px" align="center"></el-table-column>
      <el-table-column label="角色ID" prop="actor_id" width="120px" align="center"></el-table-column>
      <el-table-column label="角色名" prop="actor_name" align="center" width="90px"></el-table-column>
      <el-table-column label="清除发言" prop="clear_chat" align="center" width="60px">
        <template slot-scope="{row}">
          <span v-if="row.clear_chat === 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column label="IP地址" prop="ip" align="center" width="140px"></el-table-column>
      <el-table-column label="封禁时间(分钟)" prop="ban_time" width="100px" align="center"></el-table-column>
      <el-table-column label="封禁原因" prop="remark" width="180px" align="center"></el-table-column>
      <el-table-column label="操作时间" prop="ban_dt" width="140px" align="center"></el-table-column>
      <el-table-column label="操作人" prop="ban_nickname" width="90px" align="center"></el-table-column>
      <el-table-column label="解除时间" prop="unban_dt" width="140px" align="center"></el-table-column>
      <el-table-column label="解除人" prop="unban_nickname" width="90px" align="center"></el-table-column>
      <el-table-column align="center" label="操作" width="70px">
        <template slot-scope="{row}">
          <span v-if="row.status === 0">已解除</span>
          <el-button v-if="checkPermission(permissionRoles.ban_del) && row.status === 1" type="text" size="small" @click="handleUnban(row)">解除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="操作" prop="operate">
          <el-select v-model="dataForm.type" class="defaut-input" clearable>
            <el-option label="禁言" value="1" />
            <el-option label="封账号" value="2" />
            <el-option label="封角色" value="3" />
            <el-option label="封IP" value="4" />
            <el-option label="无感禁言" value="5" />
            <el-option label="踢玩家下线" value="6" />
          </el-select>
        </el-form-item>
        <el-form-item label="封禁目标" prop="target">
          <el-input v-model="dataForm.target" type="textarea" :autosize="{ minRows: 5, maxRows: 8}" placeholder="填写封禁目标(账号/角色ID/IP)" style="width: 50%;" />
        </el-form-item>
        <el-form-item label="封禁时间" prop="bantime">
          <el-input v-model="dataForm.bantime" class="defaut-input" placeholder="填写封禁时间*分钟" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" class="defaut-input" placeholder="备注" />
        </el-form-item>
        <el-form-item label="清除发言" prop="clearChat">
          <el-radio v-model="dataForm.clearChat" label="1">是</el-radio>
          <el-radio v-model="dataForm.clearChat" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" :disabled="buttonDisabled" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // 实现点击波浪纹
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { getServerList } from '@/api/ops'
import { ban, getBanList, unban, checkTarget } from '@/api/chat'

const defaultItem = {
  id: '',
  sid: '',
  target: '',
  bantime: '',
  clearChat: '0'
}
export default {
  name: 'Ban',
  components: { Pagination },
  directives: { waves },
  filters: {
    typeFilter(type) {
      // 固定类型（loginpay 公用类型，勿修改!!!!!!!
      const typeMap = {
        1: '禁言',
        2: '封账号',
        3: '封角色',
        4: '封IP',
        5: '无感禁言',
        6: '踢下线'
      }
      return typeMap[type]
    }
  },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      total: 0,
      loading: false,
      listLoading: false,
      dataForm: Object.assign({}, defaultItem),
      serverList: [],
      rules: {
      },
      listQuery: {
        page: 1,
        limit: 200,
        type: '',
        ban_dt: [
          new Date(Date.now() - 86400000 * 7),
          new Date()
        ]
      },
      dialogVisible: false,
      dialogType: 'new',
      buttonDisabled: false,
      textMap: {
        new: '添加封禁'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getBanList(query)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
    },
    handleFilter() {
      this.getList()
    },
    async getServerList() {
      const res = await getServerList()
      this.serverList = res.data.items
    },
    handleAdd() {
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.buttonDisabled = false
      this.getServerList()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          ban(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.getList()
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    },
    handleUnban(row) {
      this.$confirm('你确定要解除封禁吗?', '解封', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await unban(row)
          this.getList()
          this.$message({
            type: 'success',
            message: '解封成功!'
          })
        })
        .catch(err => { console.error(err) })
    },
    handleBlur(row) {
      this.buttonDisabled = true
      checkTarget(this.dataForm).then(response => {
        if (response.code === 20000) {
          this.buttonDisabled = false
        }
      })
    }
  }
}
</script>
